package me.itxuye.moment.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import butterknife.ButterKnife;
import me.itxuye.moment.utils.CommonUtils;

/**
 * Author  : itxuye(itxuye@gmail.com)|(http://itxuye.com)
 * Date    : 2015-11-30
 * Time    : 23:14
 * FIXME
 */
public abstract class BaseActivity extends AppCompatActivity
    implements View.OnClickListener {

  @Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);



    setContentView(provideContentViewId());

    ButterKnife.bind(this);

    initView();

    initData();

    setListener();
  }

  /*
     提供布局ID
  */
  abstract protected int provideContentViewId();

  /*
    toolbar相关点击事件
   */
  public void onToolbarClick() {
  }

  /*
     * 初始化控件调用的方法，建议使用
     * 查找View
     *
     * @param id   控件的id
     * @param <T> View类型
     * @return
   */
  public <T extends View> T findById(@IdRes int id) {
    return (T) findViewById(id);
  }

  //初始化view
  protected abstract void initView();

  //设置监听
  protected abstract void setListener();

  //初始化data
  protected abstract void initData();

  protected abstract void getIntentData(Bundle saveInstance);

  /**
   * startActivity
   */
  protected void readyGo(Class<?> clazz) {
    Intent intent = new Intent(this, clazz);
    startActivity(intent);
  }

  /**
   * startActivity with bundle
   */
  protected void readyGo(Class<?> clazz, Bundle bundle) {
    Intent intent = new Intent(this, clazz);
    if (null != bundle) {
      intent.putExtras(bundle);
    }
    startActivity(intent);
  }

  /**
   * startActivity then finish
   */
  protected void readyGoThenKill(Class<?> clazz) {
    Intent intent = new Intent(this, clazz);
    startActivity(intent);
    finish();
  }

  /**
   * startActivity with bundle then finish
   */
  protected void readyGoThenKill(Class<?> clazz, Bundle bundle) {
    Intent intent = new Intent(this, clazz);
    if (null != bundle) {
      intent.putExtras(bundle);
    }
    startActivity(intent);
    finish();
  }

  /**
   * startActivityForResult
   */
  protected void readyGoForResult(Class<?> clazz, int requestCode) {
    Intent intent = new Intent(this, clazz);
    startActivityForResult(intent, requestCode);
  }

  /**
   * startActivityForResult with bundle
   */
  protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
    Intent intent = new Intent(this, clazz);
    if (null != bundle) {
      intent.putExtras(bundle);
    }
    startActivityForResult(intent, requestCode);
  }

  /**
   * show toast
   */
  protected void showToast(String msg) {
    if (null != msg && !CommonUtils.isEmpty(msg)) {
      Snackbar.make(getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT).show();
    }
  }
}
